﻿Public Class frmRecursos
    Dim objBLL As New BLLRecurso
    Public tipo As String
    Public Codigo As Integer
#Region "Atualização"
    Sub CarregaCampos(ByVal codigo As Integer)
        Dim tab As DataTable
        tab = objBLL.localizar(codigo)
        txtRecurso.Text = tab.Rows(0)("rec_descricao").ToString()
        txtPatrimonio.Text = tab.Rows(0)("rec_patrimonio").ToString()
        txtObs.Text = tab.Rows(0)("rec_obs").ToString()
        cbStatus.Text = tab.Rows(0)("rec_status").ToString()
        cbStatus_SelectedIndexChanged(sender:=Nothing, e:=Nothing)
        txtMotivo.Text = tab.Rows(0)("rec_motivo").ToString()
    End Sub
    Sub Limpa()
        txtRecurso.Text = ""
        txtPatrimonio.Text = ""
        cbStatus.SelectedIndex = 0
        txtMotivo.Text = ""
        txtRecurso.Focus() 'Mantém o focu no primeiro textbox
    End Sub
    Sub Status(ByVal ret As Boolean)
        txtMotivo.Text = ""
        lbMotivo.Visible = ret
        txtMotivo.Visible = ret
    End Sub

    Sub CarregaPropriedades()
        objBLL.pcodigo = Codigo
        objBLL.pnome = Trim(txtRecurso.Text)
        objBLL.ppatrimonio = Trim(txtPatrimonio.Text)
        objBLL.pobs = Trim(txtObs.Text)
        objBLL.pstatus = cbStatus.SelectedItem
        objBLL.pmotivo = txtMotivo.Text
        objBLL.pdatacad = Trim(DateTime.Now.ToString())
    End Sub
    Sub Atualiza()
        Dim uc = New ucCadastroRecurso()
        fMenu.TPRelacionamento.Controls.Clear()
        fMenu.TPRelacionamento.Controls.Add(uc)
        uc = Nothing
    End Sub
#End Region
#Region "Validação"
    Function ValidaStatus() As Boolean
        If cbStatus.SelectedIndex = 1 Then
            If Trim(txtMotivo.Text) = "" Then
                Return False
            Else
                Return True
            End If
        Else
            Return True
        End If
    End Function
    Private Sub txtRecurso_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtRecurso.Validating
        txtRecurso.Text = StrConv(txtRecurso.Text, VbStrConv.ProperCase)
    End Sub

    Private Sub txtPatrimonio_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPatrimonio.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtMotivo_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtMotivo.Validating
        txtMotivo.Text = StrConv(txtMotivo.Text, VbStrConv.ProperCase)
    End Sub
#End Region

    Private Sub frmRecursos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Limpa()
        Select Case tipo
            Case "Novo"
                tsbAdicionar.Visible = True
                tsbAlterar.Visible = False
            Case "Alterar"
                tsbAlterar.Visible = True
                tsbAdicionar.Visible = False
                CarregaCampos(Codigo)
            Case Else

        End Select
    End Sub

    Private Sub tsbMarca_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        frmMarcas.ShowDialog()
    End Sub

    Private Sub cbStatus_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cbStatus.KeyPress
        e.Handled = True
    End Sub

    Private Sub cbStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbStatus.SelectedIndexChanged
        If cbStatus.SelectedIndex = 1 Then
            Status(True)
        Else
            Status(False)
        End If
    End Sub


    Private Sub tsbVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbVoltar.Click
        Me.Close()
    End Sub

    Private Sub tsbAdicionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAdicionar.Click
        Try
            Dim ret As Boolean
            If Trim(txtRecurso.Text) <> "" And Trim(txtPatrimonio.Text) <> "" Then
                If (ValidaStatus()) Then
                    CarregaPropriedades()
                    ret = objBLL.Inserir()
                    If ret = True Then
                        MsgBox("Recurso " & txtRecurso.Text & " Inserido com sucesso", MsgBoxStyle.OkOnly)
                        Limpa()
                        Atualiza()
                    Else
                        MsgBox("Patriônio " & txtPatrimonio.Text & " ja cadastrado", MsgBoxStyle.Information)
                        txtPatrimonio.Focus() ' mantem o focu no patrimônio
                    End If
                Else
                    MsgBox("Preencher Motivo do Desativamento", MsgBoxStyle.Information)
                End If
            Else
                MsgBox("Preencher Todos os campos Obrigatórios", MsgBoxStyle.Information)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

    Private Sub tsbAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAlterar.Click
        Try
            Dim ret As Boolean
            If Trim(txtRecurso.Text) <> "" And Trim(txtPatrimonio.Text) <> "" Then
                If (ValidaStatus()) Then
                    CarregaPropriedades()
                    ret = objBLL.Alterar()
                    If ret = True Then
                        MsgBox("Recurso: " & txtRecurso.Text & " Alterado com sucesso", MsgBoxStyle.OkOnly)
                        tsbVoltar_Click(sender, e)
                        Atualiza()
                    Else
                        MsgBox("Patriônio " & txtPatrimonio.Text & " ja cadastrado", MsgBoxStyle.Information)
                        txtPatrimonio.Focus() ' mantem o focu no solicitante
                    End If
                Else
                    MsgBox("Preencher Motivo do Desativamento", MsgBoxStyle.Information)
                End If
            Else
                MsgBox("Preencher Todos os campos Obrigatórios", MsgBoxStyle.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub

End Class